<template>
  <!-- 批量导入 -->
  <!--模态窗 单行-->
  <transition
    name="custom-classes-transition"
    enter-active-class="animated fadeInDown"
    leave-active-class="animated fadeOutUp"
  >

    <div class="modal-ctrl-body" v-show="bulkImportObj.isShow">
      <div class="modal-ctrl-main odd-row-ctrl">
        <div class="modal-title">
          <p>{{ bulkImportObj.title }}</p>
        </div>
        <div class="modal-ctrl-content">
          <div class="modal-row">
            <div class="modal-form-key f-l">
              导入：
            </div>
            <input type="file" title="选择文件" ref="formFile" @change="fileUser" class="modal-text f-l file-btn">
            <span class="file-user-btn">
              选择文件:{{fileName}}
            </span>
          </div>
          <div class="modal-row">
            <div class="modal-form-key f-l">
              &nbsp;
            </div>
            <a href="#" class="f-l download-a" @click="getModal">模板下载</a>
          </div>
        </div>
        <div class="btns text-r">
          <button type="button" class="btn btn-default" @click="close">取消</button>
          <button type="button" class="btn btn-info" @click="subBtn">提交</button>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
    export default {
      name: "BulkImport",
      props:["bulkImportObj"],
      data(){
        return{
          userModalFile:"",
          fileName:""
        }
      },
      methods:{
        fileUser(ele){

          let type = ele.target.files[0].name.split(".")[1];
          if( this.verifyForm.isXls(type) ){
            this.userModalFile = ele.target.files[0];
            this.fileName = ele.target.files[0].name;
          }else{
            this.s_alert("选择文件不是所下载的模板文件！");
            this.$refs.formFile.value = "";
            this.fileName = "";
          }

        },
        subBtn(){
          // let configFile = {
          //   headers:{'Content-Type':'multipart/form-data'}
          // };
          this.s_loading(true);
          //console.log(this.bulkImportObj);
          if( this.userModalFile ){
            let urlType = this.bulkImportObj.urlType;
            let url = "";

            switch (urlType){
              case "dot":
                url = this.DotCtrl + this.bulkImportObj.upUrl;
                break;
              case "admin":
                url = this.HOST + this.bulkImportObj.upUrl;
                break;
              case "ControlObject":
                url = this.ControlObject + this.bulkImportObj.upUrl +"?userId="+this.isLogin().userId;
                break;
            }

            let param = new FormData();
            param.append('file',this.userModalFile);
            //POST /bs/batch/policeInfo
            this.$axios.post(url ,param)
              .then((data)=>{
                if( urlType === "dot" ){
                  if( data ){
                    this.s_alert("上传成功！");
                    switch (this.bulkImportObj.upUrl){
                      case "/bs/batch/policeInfoAndUser":
                        this.s_logwrite("基础信息管理-用户管理","导入警员用户");
                        break;
                      case "/bs/batch/policeInfo":
                        this.s_logwrite("基础信息管理-警员管理","导入警员");
                        break;
                      case "/policeFacility/BSBatch/carInfo":
                        this.s_logwrite("资源信息管理-警车管理","导入警车");
                        break;
                      case "/policeFacility/BSBatch/radio":
                        this.s_logwrite("资源信息管理-手台管理","导入手台");
                        break;
                      case "/policeFacility/BSBatch/danbing":
                        this.s_logwrite("资源信息管理-单兵管理","导入单兵");
                        break;
                      case "/policeFacility/BSBatch/jwt":
                        this.s_logwrite("资源信息管理-单兵管理","导入警务通");
                        break;
                      case "/camera/upExcel":
                        this.s_logwrite("资源信息管理-摄像头管理","导入摄像头");
                        break;
                      case "/resource/wifiCRUD/upExcel":
                        this.s_logwrite("资源信息管理-摄像头管理","导入WIFI");
                        break;
                      case "/resource/addpersonStream/upExcel":
                        this.s_logwrite("资源信息管理-摄像头管理","导入人员流动");
                        break;
                    }
                    this.close();
                  }
                }else {
                  if( this.bulkImportObj.urlType === "ControlObject" ){
                    if( data.data.length > 0 ){
                      this.s_alert("上传成功！");
                      this.close();
                    }else{
                      this.s_alert("上传失败！");
                    }
                  }else {
                    if( data.data.success === true ){
                      this.s_alert("上传成功！");
                      this.close();
                    }else{
                      this.s_alert(data.data.errorMsg );
                    }
                  }

                }

                this.s_loading(false);

                //console.log(data);
              })
              .catch((err)=>{
                let errArr = err.response.data.errorMsg.split("|");
                //console.log(errArr[1]);
                if( errArr[1] ){
                  this.s_alert(errArr[1].substr(0,errArr[1].length-2));
                }else if( err.response.data.errorMsg ){
                  this.s_alert(err.response.data.errorMsg);
                }else{
                  this.s_alert("上传失败！");
                }
                this.s_loading(false);

              });
          }else{
            this.s_alert("请选择需要导入的文件！");
          }

        },
        //获取用户模板
        getModal(){
          let urlType = this.bulkImportObj.urlType;
          switch (urlType){
            case "dot":
              window.location.href = this.DotCtrl + this.bulkImportObj.TemplateUrl;
              break;
            case "admin":
              window.location.href = this.HOST + this.bulkImportObj.TemplateUrl;
              break;
            case "ControlObject":
              window.location.href = this.ControlObject + this.bulkImportObj.TemplateUrl;
              break;
          }
        },
        close(){
          this.$emit("upModalClose");
          this.$refs.formFile.value = "";
          this.userModalFile = "";
          this.fileName = "";
        }
      }
    }
</script>

<style scoped>
  .fadeInDown,.fadeOutUp{
    animation-duration: .500s;
  }
  .download-a{
    color: #0f76d8;
    text-decoration: underline ;
  }
  .file-btn{
    position: absolute;
    width: 80%;
    height: 30px;
    z-index: 2;
    opacity: 0;
    right: 0;
  }
  .file-user-btn{
    position: absolute;
    width: 80%;
    height: 30px;
    line-height: 30px;
    text-align: left;
    box-sizing: border-box;
    padding-left: 10px;
    color: #fff;
    font-size: 16px;
    right: 0;
    background-color: #5bc0de;
    border: 1px solid #ededed;
    border-radius: 4px;
  }
</style>
